Welcome to pandas!

5 日期处理技术

日期时间也是数据分析处理时的重要对象,Excel中有很多功能都有提供关于日期时间的处理,在Pandas中,得提供相关一些处理函数,本节主要学习关于时间、时间差的数据转换、运算处理。

1.5.1单个时间戳的创建

时间戳是具有时区支持的特定日期和时间,是一个表示在某个特定时间之前存在的、完整的、可验证的数据。在当前绝大部分计算机系统中,时间戳是从格林尼治时间1970年01月01日00时00分00秒(北京时间:1970年01月01日08时00分00秒)开始。

在pandas中创建时间戳是使用pd.Timestamp()函数和pd.to_datetime()函数,如果获取当前的计算机系统时间戳,则只能导入datetime标准库,该库在的datmtime.now()函数可获取当前系统时间戳。


import pandas as pd

t1=pd.Timestamp(2024,2,28,20,26,26)

t2=pd.Timestamp("2024-2-28 20:26:59")

t3=pd.to_datetime("2024-2-29 12:12:12")

print (t1)

print (t2)

print (t3)

返回:

2024/2/28 20:26:26

2024/2/28 20:26:59

2024/2/29 12:12:12


import pandas as pd,datetime as dt

t1=dt.datetime.now()

print (t1)

返回:

2024-02-28 20:33:23.838148 #最后一个数据是毫秒